﻿
Partial Class WebForm_ManageVOrganization_AddVOrganization
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = False

        If Not IsPostBack Then

        End If
    End Sub

    'Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    '    If Me.DataAccessMode = My.DataAccessMode.Insert Then
    '        Me.AddData()
    '        RaiseEvent OrganizationAdded(Me, New EventArgs)
    '    ElseIf Me.DataAccessMode = My.DataAccessMode.Update Then
    '        Me.UpdateData()
    '        RaiseEvent OrganizationUpdated(Me, New EventArgs)
    '    End If
    'End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindDDLActive()
        Me.BindDDLOrganizationType()
        Me.BindDDLIsGov()
        Me.BindDDLRegion()
        Me.BindDDLMOIOrganizationType()

        If Me.fv.CurrentMode = FormViewMode.Edit Then
            Dim ddlOrganizationType As DropDownList = TryCast(Me.fv.FindControl("ddlOrganizationType"), DropDownList)

            ddlOrganizationType.SelectedValue = DataBinder.Eval(Me.fv.DataItem, "OrganizationType")

            If DataBinder.Eval(fv.DataItem, "IsGov") Is DBNull.Value Then
                TryCast(Me.fv.FindControl("ddlIsGov"), DropDownList).SelectedValue = ""
            Else
                TryCast(Me.fv.FindControl("ddlIsGov"), DropDownList).SelectedValue = DataBinder.Eval(fv.DataItem, "IsGov")
            End If

            If DataBinder.Eval(fv.DataItem, "RegionNO") Is DBNull.Value Then
                TryCast(Me.fv.FindControl("ddlRegion"), DropDownList).SelectedValue = ""
            Else
                TryCast(Me.fv.FindControl("ddlRegion"), DropDownList).SelectedValue = DataBinder.Eval(fv.DataItem, "RegionNO")
            End If

            If DataBinder.Eval(fv.DataItem, "MOIOrganizationType") Is DBNull.Value Then
                TryCast(Me.fv.FindControl("ddlMOIOrganizationType"), DropDownList).SelectedValue = ""
            Else
                TryCast(Me.fv.FindControl("ddlMOIOrganizationType"), DropDownList).SelectedValue = DataBinder.Eval(fv.DataItem, "MOIOrganizationType")
            End If

        End If
    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.DataAccessMode = My.DataAccessMode.Insert Then
                Me.AddData()
                RaiseEvent OrganizationAdded(Me, New EventArgs)
            ElseIf Me.DataAccessMode = My.DataAccessMode.Update Then
                Me.UpdateData()
                RaiseEvent OrganizationUpdated(Me, New EventArgs)
            End If

            RaiseEvent Saved(Me, New EventArgs)
        End If
    End Sub
End Class

Partial Class WebForm_ManageVOrganization_AddVOrganization
    Private Function AddData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("insert VOrganization (VOrganizationName,Parent,OrganizationID,Active,OrganizationType,IsGov,Phone,CityNO,RegionNO,Address,Introduction,EstablishDate,MOIOrganizationType) select ")
            sql.AppendFormat(" '{0}' ", TryCast(Me.fv.FindControl("txtVOrganizationName"), TextBox).Text)
            sql.AppendFormat(",{0} ", Me.ParentID)
            sql.AppendFormat(",{0} ", Me.OrganizationID)
            sql.AppendFormat(",'{0}' ", "Y")
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlOrganizationType"), DropDownList).SelectedValue)
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlIsGov"), DropDownList).SelectedValue)
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtPhone"), TextBox).Text)
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlCity"), DropDownList).SelectedValue)
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlRegion"), DropDownList).SelectedValue)
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtAddress"), TextBox).Text)
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtIntroduction"), TextBox).Text)
            sql.AppendFormat(",'{0:yyyy/MM/dd}' ", DateTime.Parse(TryCast(Me.fv.FindControl("txtBirthday"), WebForm_CommonControl_CalendarTextBox).Text))
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlMOIOrganizationType"), DropDownList).SelectedValue)

            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            Me.VOrganizationID = MyObj.GetIdentity()
            Me.VOrganizationName = TryCast(Me.fv.FindControl("txtVOrganizationName"), TextBox).Text

            clsAuth.BuildVOrganizationCode()

            scope.Complete()
        End Using

        Return True
    End Function

    Private Function UpdateData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("update VOrganization set ")
            sql.AppendFormat(" VOrganizationName = '{0}' ", TryCast(Me.fv.FindControl("txtVOrganizationName"), TextBox).Text)
            sql.AppendFormat(",Active = '{0}' ", TryCast(Me.fv.FindControl("ddlActive"), DropDownList).SelectedValue)
            sql.AppendFormat(",IsGov = '{0}' ", TryCast(Me.fv.FindControl("ddlIsGov"), DropDownList).SelectedValue)
            sql.AppendFormat(",Phone = '{0}' ", TryCast(Me.fv.FindControl("txtPhone"), TextBox).Text)
            sql.AppendFormat(",CityNO = '{0}' ", TryCast(Me.fv.FindControl("ddlCity"), DropDownList).SelectedValue)
            sql.AppendFormat(",RegionNO = '{0}' ", TryCast(Me.fv.FindControl("ddlRegion"), DropDownList).SelectedValue)
            sql.AppendFormat(",Address = '{0}' ", TryCast(Me.fv.FindControl("txtAddress"), TextBox).Text)
            sql.AppendFormat(",Introduction = '{0}' ", TryCast(Me.fv.FindControl("txtIntroduction"), TextBox).Text)
            sql.AppendFormat(",EstablishDate = '{0:yyyy/MM/dd}' ", DateTime.Parse(TryCast(Me.fv.FindControl("txtBirthday"), WebForm_CommonControl_CalendarTextBox).Text))
            sql.AppendFormat(",MOIOrganizationType = '{0}' ", TryCast(Me.fv.FindControl("ddlMOIOrganizationType"), DropDownList).SelectedValue)
            sql.AppendFormat("where VOrganizationID = {0} ", Me.VOrganizationID)

            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            Me.VOrganizationName = TryCast(Me.fv.FindControl("txtVOrganizationName"), TextBox).Text

            clsAuth.BuildVOrganizationCode()

            scope.Complete()
        End Using

        Return True
    End Function

    Private Sub BindDDLActive()
        Dim ddlActive As DropDownList = TryCast(Me.fv.FindControl("ddlActive"), DropDownList)

        ddlActive.DataTextField = "Text"
        ddlActive.DataValueField = "Value"
        ddlActive.DataSource = MyObj.GetCodeMappingData("Organization", "Active")
        ddlActive.DataBind()

        'ddlActive.SelectedIndex = ddlActive.Items.IndexOf(ddlActive.Items.FindByValue("Y"))
    End Sub

    Private Sub BindDDLOrganizationType()
        Dim ddlOrganizationType As DropDownList = TryCast(Me.fv.FindControl("ddlOrganizationType"), DropDownList)

        Dim dt As Data.DataTable = MyObj.GetCodeMappingData("Organization", "OrganizationType")
        dt.DefaultView.RowFilter = "Value <> 'O' "

        ddlOrganizationType.DataTextField = "Text"
        ddlOrganizationType.DataValueField = "Value"
        ddlOrganizationType.DataSource = dt.DefaultView
        ddlOrganizationType.DataBind()

    End Sub

    Private Sub BindDDLMOIOrganizationType()
        Dim ddlMOIOrganizationType As DropDownList = TryCast(Me.fv.FindControl("ddlMOIOrganizationType"), DropDownList)

        ddlMOIOrganizationType.DataTextField = "Text"
        ddlMOIOrganizationType.DataValueField = "Value"
        ddlMOIOrganizationType.DataSource = MyObj.GetCodeMappingData("Organization", "MOIOrganizationType")
        ddlMOIOrganizationType.DataBind()

        ddlMOIOrganizationType.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Private Sub BindDDLIsGov()
        Dim ddlIsGov As DropDownList = TryCast(Me.fv.FindControl("ddlIsGov"), DropDownList)

        ddlIsGov.DataTextField = "Text"
        ddlIsGov.DataValueField = "Value"
        ddlIsGov.DataSource = MyObj.GetCodeMappingData("Organization", "IsGOV")
        ddlIsGov.DataBind()

        ddlIsGov.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Private Sub BindDDLRegion()
        Dim ddlRegion As DropDownList = TryCast(Me.fv.FindControl("ddlRegion"), DropDownList)

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" select * from Region where RegionType='T' ")

        ddlRegion.DataTextField = "RegionName"
        ddlRegion.DataValueField = "RegionID"
        ddlRegion.DataSource = dat.GetData(sql)
        ddlRegion.DataBind()

        ddlRegion.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    ''' <summary>
    ''' 新增模式
    ''' </summary>
    ''' <param name="organizationID"></param>
    ''' <param name="parentID"></param>
    ''' <remarks></remarks>
    Public Sub Show(ByVal organizationID As String, ByVal parentID As String)
        Me.DataAccessMode = My.DataAccessMode.Insert
        Me.fv.ChangeMode(FormViewMode.Insert)
        Me.ParentID = parentID
        Me.OrganizationID = organizationID

        'Me.mpSearch.Show()
        Me.Visible = True

        '父單位為民間團體
        If organizationID = "null" Then
            TryCast(Me.fv.FindControl("ddlOrganizationType"), DropDownList).Enabled = False
            TryCast(Me.fv.FindControl("ddlOrganizationType"), DropDownList).SelectedValue = "V"
        Else
            TryCast(Me.fv.FindControl("ddlOrganizationType"), DropDownList).Enabled = True
        End If

    End Sub

    ''' <summary>
    ''' 編輯模式
    ''' </summary>
    ''' <param name="vorganizationID"></param>
    ''' <remarks></remarks>
    Public Sub ShowEdit(ByVal vorganizationID As String)
        Me.DataAccessMode = My.DataAccessMode.Update
        Me.fv.ChangeMode(FormViewMode.Edit)
        Me.VOrganizationID = vorganizationID

        Me.BindDataForEdit()

        'Me.mpSearch.Show()
        Me.Visible = True

        'TryCast(Me.fv.FindControl("ddlOrganizationType"), DropDownList).Enabled = True
    End Sub

    Private Sub BindDataForEdit()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select o.* from VOrganization o ")
        sql.AppendFormat("where o.VOrganizationID = {0} ", Me.VOrganizationID)

        Dim dt As Data.DataTable = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0)

        Me.fv.DataSource = dt
        Me.fv.DataBind()

        'TryCast(Me.fv.FindControl("txtVOrganizationName"), TextBox).Enabled = False
        TryCast(Me.fv.FindControl("ddlActive"), DropDownList).Enabled = False
        TryCast(Me.fv.FindControl("ddlActive"), DropDownList).SelectedValue = dt.Rows(0)("Active")

    End Sub

    Private Property ParentID() As String
        Get
            Return ViewState("ParentID")
        End Get
        Set(ByVal value As String)
            ViewState("ParentID") = value
        End Set
    End Property

    Private Property OrganizationID() As String
        Get
            Return ViewState("OrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("OrganizationID") = value
        End Set
    End Property

    Public Property VOrganizationID() As String
        Get
            Return ViewState("VOrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("VOrganizationID") = value
        End Set
    End Property

    Public Property VOrganizationName() As String
        Get
            Return ViewState("VOrganizationName")
        End Get
        Set(ByVal value As String)
            ViewState("VOrganizationName") = value
        End Set
    End Property

    Public Event OrganizationAdded(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event OrganizationUpdated(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event Saved(ByVal sender As Object, ByVal e As System.EventArgs)

    Private Property DataAccessMode() As My.DataAccessMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property
End Class